package com.example.mymnue;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DetailActivity extends AppCompatActivity implements Runnable{
    private static final String TAG= "AA";
    String dz,title,detail1;
    Handler handler;
    private List<HashMap<String, Object>> listItems; // 存放文字、图片信息
    private SimpleAdapter simpleAdapter; // 适配器
    private int msgWhat = 7;
    HashMap<String, Object> map;
    SharedPreferences sp;
    private ShopDB db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        sp=getSharedPreferences("caidan", Activity.MODE_PRIVATE);
        sp.getString("title",null);
        sp.getString("detail",null);
        Intent detail =getIntent();
        dz = detail.getStringExtra("DZ");
        title=detail.getStringExtra("title");
        detail1=detail.getStringExtra("yuanliao");
        Log.i(TAG, "onCreate: dz= "+dz);
        ListView dlist=(ListView) findViewById(R.id.dlist);
        initListView();
        dlist.setAdapter(simpleAdapter);
        Thread thread=new Thread(this);
        thread.start();
        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what==msgWhat){
                    listItems=(List<HashMap<String, Object>>) msg.obj;
                    dlist.setAdapter(new SimpleAdapter(DetailActivity.this, listItems,
                            R.layout.list_detail,
                            new String[]{"img", "head"},
                            new int[]{R.id.imageView2, R.id.textView5}
                    ));
                }
                super.handleMessage(msg);
            }
        };
    }
    private void initListView() {
        listItems = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 10; i++) {
            map = new HashMap<String, Object>();
            map.put("head", "顺序 " + i); // 详情描述
            listItems.add(map);
        }
        // 生成适配器的Item和动态数组对应的元素
        simpleAdapter = new SimpleAdapter(this, listItems, // listItems数据源
                R.layout.list_detail, // ListItem的XML布局实现
                new String[] {"img","head"},
                new int[] {  R.id.imageView2,R.id.textView5 }
        );
    }

    @Override
    public void run() {
        listItems=new ArrayList<HashMap<String,Object>>();
        int[] img ={R.drawable.step1,R.drawable.step2,R.drawable.step3,R.drawable.step4,R.drawable.step5,R.drawable.step6,R.drawable.step7,
                R.drawable.step8,R.drawable.step9,R.drawable.step10,R.drawable.step11,R.drawable.step12,R.drawable.step13,
        R.drawable.step14,R.drawable.step15,R.drawable.step16,R.drawable.step17};
        try {
            Document document = Jsoup.connect(dz).get();
            Elements tds = document.getElementsByClass("recipeStep_word");
            Log.i(TAG, "run: tds="+tds);
            for (int i =0;i<tds.size();i++){
                Element divf = document.getElementsByClass("recipeStep_word").get(i);
                Log.i(TAG, "run: divf="+divf.text());
                map = new HashMap<String, Object>();
                map.put("head",divf.text());
                map.put("img",img[i]);
                listItems.add(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Message msg=new Message();
        msg.what=msgWhat;
        msg.obj=listItems;
        handler.sendMessage(msg);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.shop:
                Intent intent1=new Intent(DetailActivity.this,ShopActivity.class);
                startActivity(intent1);
                break;
            case R.id.history:
                Intent intent2 =new Intent(DetailActivity.this,HistoryActivity.class);
                startActivity(intent2);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void btn(View v){

        AlertDialog.Builder builder=new AlertDialog.Builder(DetailActivity.this);
        builder.setTitle("提示")
                .setMessage("已经成功将"+title+"加入菜单")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sp=getSharedPreferences("caidan",Activity.MODE_PRIVATE);
                        SharedPreferences.Editor editor= sp.edit();
                        editor.putString("title",title);
                        editor.putString("detail",detail1);
                        editor.commit();
                        CaidanItem item=new CaidanItem();
                        String curDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
                        String dtime="早";
                        Log.i(TAG, "onClick: data"+curDateStr);
                        CaidanDBManager caidanDBManager= null;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
                            caidanDBManager = new CaidanDBManager(DetailActivity.this);
                        }
                        item.setDdata(curDateStr);
                        item.setDtime(dtime);
                        item.setDtitle(title);
                        item.setDdtail(detail1);
                        Log.i(TAG, "onClick: item="+item);
                        caidanDBManager.add(item);
                        Intent intent=new Intent(DetailActivity.this,DishActivity.class);
                        setResult(2,intent);
                        finish();
                    }
                })
                .create().show();
    }




}